package com.neborosoft.lib.collections.single;

/**
 * Created with IntelliJ IDEA.
 * User: CM
 * Date: 01.11.12
 * Time: 23:36
 * To change this template use File | Settings | File Templates.
 */
public class PeriodicMutableValue<T> {
    private T value;
    private long lastTime = System.currentTimeMillis();
    private long mutationDuration;
    private T mutateTo;

    public PeriodicMutableValue(T value, T mutateTo, long mutationDurationMilliseconds) {
        this.mutateTo = mutateTo;
        this.mutationDuration = mutationDurationMilliseconds;
        this.value = value;
    }

    public T get() {
        long curTime = System.currentTimeMillis();
        long timeElapsed = curTime - lastTime;
        if(timeElapsed >= mutationDuration){
            value = mutateTo;
            lastTime = curTime;
        }
        return value;
    }

    public long getMutationDuration() {
        return mutationDuration;
    }

    public void setMutationDuration(long mutationDuration) {
        this.mutationDuration = mutationDuration;
    }

    public void set(T value) {
        this.value = value;
        lastTime = System.currentTimeMillis();
    }

    public T getMutateTo() {
        return mutateTo;
    }

    public void setMutateTo(T mutateTo) {
        this.mutateTo = mutateTo;
    }
}
